﻿function _MessageBox() {
    var that = this;
    /*
    * @method
    * @param {Msg:消息内容} option基本参数
    * @returns void
    * @desc 弹出内容
    */
    this.Show = function (option) {
        var _option = {
            Type: "alert",
            Msg: "",
            IfDebug: false
        };
        if (typeof option == "undefined") {
            return;
        }
        if (typeof option == "string") {
            _option.Msg = option;
            layer.msg(option);
            return;
        }
        if (typeof option.Msg == "string") {
            _option.Msg = option.Msg;
        }
        if (typeof option.IfDebug != "boolean") {
            _option.IfDebug = option.IfDebug;
        }
        if (typeof option.Type != "undefined") {
            _option.Type = option.Type;
        }

        switch (_option.Type) {
            case "alert":
                {
                    var data = {
                        content: "",
                        options: undefined,
                        yes: undefined
                    };
                    data.options = option.options;
                    data.content = _option.Msg;
                    data.yes = option.YesCallBack;
                    layer.alert(data.content, data.options, data.yes);
                }
                return;
            case "msg":
                layer.msg(_option.Msg);
                return;
            case "confirm":
                {
                    var l_option = {
                        Content: "",
                        options: {},
                        yes: undefined,
                        cancel: undefined
                    };
                    l_option.Content = _option.Msg;
                    if (typeof option.YesCallback != "undefined") {
                        l_option.yes = option.YesCallback;
                    }
                    if (typeof option.CancelCallback != "undefined") {
                        l_option.cancel = option.CancelCallback;
                    }
                    if (typeof option.Title == "string") {
                        l_option.options.title = option.Title;
                    }
                    layer.confirm(l_option.Content, l_option.options, l_option.yes, l_option.cancel);
                }
                return;
            default:
                layer.msg("【MessageBox.Show】异常参数 Type");
                return;
        }
    };
    /**
     * @method
     * @param option 基本参数
     * @return void
     * @desc 加载页面
     */
    this.Load = function (option) {
        if (typeof option == "undefined") {
            that.Show({ Msg: "【MessageBox.Load】 请传入参数 主体" });
            return;
        }
        if (typeof option.Url == "undefined") {
            that.Show({ Msg: "【MessageBox.Load】 请传入参数 Url" });
            return;
        }

        if (typeof option.CloseBtn == "undefined") {
            option.CloseBtn = 1;
        }

        if (typeof option.Callback != "undefined") {
            console.log(option);
            /*
            * @param index 当前子页面IframeIndex
            * @param layero 当前弹出层DOM
            * @return void
            * @desc 操作回调
            */
            that.Alert("此属性已作废！请根据方法新参数修改程序逻辑。");
            return;
        }
        /*
        if (typeof option.Area == "undefined") {
        option.Area = null;
        }
        if (typeof option.Title == "undefined") {
        option.Title = null;
        }
        if (typeof option.CancelCallback == "undefined") {
        option.CancelCallback = function (index) {
        console.log("xx");
        option.Callback(index, null);
        return true;
        };
        }
        if (typeof option.SuccessCallback == "undefined") {
        option.SuccessCallback = option.Callback;
        }
        */
        if (typeof option.Scrollbar != "boolean") {
            option.Scrollbar = false;
        }
        if (typeof option.Resize != "boolean") {
            option.Resize = true;
        }

        /*
        if (typeof option.EndCallBack == "undefined") {
        option.End = undefined;
        }
        */

        layer.open({
            type: 2, //基本层类型
            closeBtn: option.CloseBtn,
            content: (option.Scrollbar ? [option.Url, "no"] : option.Url), //Url
            title: option.Title == null ? "" : option.Title, //标题
            area: option.Area == null ? "auto" : option.Area, //宽高
            //id: "MODAL_LAYER", //用于控制弹层唯一标识
            move: option.move,
            resize: option.Resize, //是否允许拉伸
            scrollbar: option.Scrollbar, //是否允许浏览器出现滚动条
            cancel: option.CancelCallback, //, 右上角关闭按钮触发的回调
            yes: option.Callback,
            success: option.SuccessCallback,
            shadeClose: option.shadeClose,
            maxmin: option.maxmin,
            end: option.end //层销毁后触发的回调
        });
    };
    this.Print = function (option) {
        /*
        var url = "/BusinessExpansion/Print/PublicPrint.aspx";

        window.showModalDialog(url, window);
        return;
        */
        if (typeof option.Url == "undefined") {
            option.Url = that.Config.BasicPrint;
        }
        if (typeof option.Value == "string") {
            option.Url += "?" + option.Value;
        }
        if (typeof option.Title == "undefined") {
            option.Title = "打印";
        }
        if (typeof option.Area == "undefined") {
            option.Area = ["800px", "500px"];
        }
        if (typeof option.Type == "string") {
            if (option.Type == "Content") {
                option.Url = option.Content;
                layer.open({
                    type: 1, //基本层类型
                    content: option.Content, //Url
                    title: option.Title, //标题
                    area: option.Area, //宽高
                    //id: "MODAL_LAYER", //用于控制弹层唯一标识
                    //move: false,
                    resize: true, //是否允许拉伸
                    scrollbar: true, //是否允许浏览器出现滚动条
                    cancel: undefined, //, 右上角关闭按钮触发的回调
                    yes: undefined,
                    success: function (index, ifromDom) {
                    },
                    end: undefined //层销毁后触发的回调
                });
                return;
            }
        }
        console.log(option);
        layer.open({
            type: 2, //基本层类型
            content: option.Url, //Url
            title: option.Title, //标题
            area: option.Area, //宽高
            //id: "MODAL_LAYER", //用于控制弹层唯一标识
            //move: false,
            resize: true, //是否允许拉伸
            scrollbar: true, //是否允许浏览器出现滚动条
            cancel: undefined, //, 右上角关闭按钮触发的回调
            yes: undefined,
            success: function (index, ifromDom) {
            },
            end: undefined //层销毁后触发的回调
        });
        //that.Load(option);
    }
    //选择公司Modal
    this.OpenCompany = function (option) {
        if (typeof option.Window == "undefined") {
            option.Window = null;
            that.Show({ Msg: "【MessageBox.OpenCompany】 请传入参数 Window" });
            return;
        }

        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined" || typeof option.ValueID == "undefined") {
                that.Show({ Msg: "【MessageBox.OpenCompany】 请传入参数 TextID" });
                return;
            }
            option.CallBack = function (index) {
                var data = window.frames["layui-layer-iframe" + index].ModalData;

                if (!(data.Text == "" && option.Window.document.getElementById(option.TextID).value != "请选择公司")) {
                    if (data.Text == "") {
                        data.Text = "请选择公司";
                    }
                    option.Window.document.getElementById(option.TextID).value = data.Text;
                    option.Window.document.getElementById(option.ValueID).value = data.Value;
                }
                return true;
            };
        }

        var urlval = "?";

        if (typeof option.Value == "undefined") {
            option.Value = null;
        }
        else {
            urlval += "comId=" + option.Value + "&";
        }
        if (typeof option.OneCheck == "undefined") {
            option.OneCheck = null;
        }
        else {
            urlval += "check=" + option.OneCheck + "&"
        }

        urlval = urlval.substr(0, urlval.length - 1)

        that.Load({
            Url: that.Config.CheckCompanyUrl + urlval,
            Title: "选择公司",
            Area: ['550px', '350px'],
            CancelCallback: option.CallBack
        });
    }
    /**
     * @method
     * @param option 参数
     * @return void
     * @desc 选择用户
     */
    this.OpenUser = function (option) {
        //console.log(option);
        if (typeof option.Window == "undefined") {
            option.Window = null;
            that.Show({ Msg: "请传入系统参数" });
            return;
        }
        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined") {
                option.TextID = "Names";
            }
            if (option.ValueID == "undefined") {
                option.ValueID = "HIds";
            }

            option.CallBack = function (index) {
                try {
                    var data = window.frames["layui-layer-iframe" + index].ModalData;
                    if (data.Text == "") {
                        data.Text = "请选择用户";
                    }
                    option.Window.document.getElementById(option.TextID).value = data.Text;
                    option.Window.document.getElementById(option.ValueID).value = data.Value;
                } catch (e) {
                }
                return true;
            };
        }
        if (typeof option.Url == 'undefined') {
            option.Url = that.Config.CheckUser;
        }

        if (typeof option.Value == 'string') {
            option.Url += option.Value;
        }
        that.Load({
            Url: option.Url,
            Title: "选择用户",
            Area: ['700px', '600px'],
            CancelCallback: option.CallBack
        });
    };
    this.OpenCustomer = function (option) {
        if (typeof option.Window == "undefined") {
            option.Window = null;
            that.Show({ Msg: "请传入系统参数" });
            return;
        }
        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined" || typeof option.ValueID == "undefined") {
                that.Show({ Msg: "【MessageBox.OpenRole】 请传入参数 TextID" });
                return;
            }
            option.CallBack = function (index) {
                try {
                    var data = window.frames["layui-layer-iframe" + index].ModalData;
                    if (data.Text == "") {
                        data.Text = "请选择客户";
                        data.Value = "-1";
                    }
                    option.Window.document.getElementById(option.TextID).value = data.Text;
                    option.Window.document.getElementById(option.ValueID).value = data.Value;
                    return true;
                } catch (e) {
                    that.Show({ Msg: "【MessageBox.OpenRole】 异常：" + e.toString() });
                    return false;
                }
            };
        }
        if (typeof option.Value == "undefined") {
            option.Value = "";
        }
        that.Load({
            Url: "/BusinessExpansion/PublicShow/SelectCustomer.aspx" + option.Value,
            Title: "选择客户公司",
            Area: ['700px', '600px'],
            CancelCallback: option.CallBack
        });
    }
    /**
     * @method
     * @param option 参数
     * @return void
     * @desc 选择角色
     */
    this.OpenRole = function (option) {
        console.log(option);
        if (typeof option.Window == "undefined") {
            option.Window = null;
            that.Show({ Msg: "请传入系统参数" });
            return;
        }
        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined" || typeof option.ValueID == "undefined") {
                that.Show({ Msg: "【MessageBox.OpenRole】 请传入参数 TextID" });
                return;
            }
            option.CallBack = function (index) {
                try {
                    var data = window.frames["layui-layer-iframe" + index].ModalData;

                    if (data.Text != "" || option.Window.document.getElementById(option.TextID).value == "请选择角色") {
                        if (data.Text == "") {
                            data.Text = "请选择角色";
                        }
                        option.Window.document.getElementById(option.TextID).value = data.Text;
                        option.Window.document.getElementById(option.ValueID).value = data.Value;
                    }

                    return true;
                } catch (e) {
                    that.Show({ Msg: "【MessageBox.OpenRole】 异常：" + e.toString() });
                    return true;
                }
            };
        }
        console.log(option);
        if (option.Type == 2) {
            that.Load({
                Url: that.Config.CheckRoleTree,
                Title: "选择角色",
                Area: ['650px', '450px'],
                CancelCallback: option.CallBack
            });
            return;
        }
        if (option.Type == 3) {
            that.Load({
                Url: "/BusinessExpansion/Business/SelectRole.aspx",
                Title: "选择角色",
                Area: ['850px', '550px'],
                CancelCallback: option.CallBack
            });
            return;
        }
        that.Load({
            Url: that.Config.CheckRole,
            Title: "选择角色",
            Area: ['650px', '450px'],
            CancelCallback: option.CallBack
        });
    };

    // 选择关键字
    this.OpenCode = function (option) {
        if (typeof option == "undefined") {
            that.Alert("【MessageBox.OpenCode】请传入参数");
            return;
        }
        if (typeof option.Window == "undefined") {
            that.Alert("【MessageBox.OpenCode】请传入Window参数");
            return;
        }
        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined" || typeof option.ValueID == "undefined") {
                that.Show({ Msg: "【MessageBox.OpenCode】 请传入参数 TextID" });
                return;
            }
            option.CallBack = function (index) {
                try {
                    var data = window.frames["layui-layer-iframe" + index].ModalData;

                    if (!(data.Text == "" && option.Window.document.getElementById(option.TextID).value != "请选择关键字")) {
                        if (data.Text == "") {
                            data.Text = "请选择关键字";
                        }
                        option.Window.document.getElementById(option.TextID).value = data.Text;
                        option.Window.document.getElementById(option.ValueID).value = data.Value;
                    }
                    return true;
                } catch (e) {
                    that.Show({ Msg: "【MessageBox.OpenCode】 异常：" + e.toString() });
                    return true;
                }
            };
        }

        if (typeof option.PROID == undefined || option.PROID == 0) {
            that.Alert("请选择一个项目！");
            return;
        }

        if (option.Type == 2) {
            that.Load({
                Url: (option.PROID == undefined ? that.Config.CheckCode : (that.Config.CheckCode + "?PROID=" + option.PROID)),
                Title: "选择关键字",
                Area: ['650px', '550px'],
                CancelCallback: option.CallBack
            });
            return;
        }
        if (option.Type == 3) {
            that.Load({
                Url: "/BusinessExpansion/PublicShow/CreateCode.aspx",
                Title: "选择关键字",
                Area: ['850px', '550px'],
                CancelCallback: option.CallBack
            });
            return;
        }
    }

    //打开选择模板
    this.OpenBlock = function (option) {
        if (typeof option == "undefined") {
            that.Alert("【MessageBox.OpenBlock】请传入参数");
            return;
        }
        if (typeof option.Window == "undefined") {
            that.Alert("【MessageBox.OpenBlock】请传入Window参数");
            return;
        }
        if (typeof option.CallBack == "undefined") {
            if (typeof option.TextID == "undefined" || typeof option.ValueID == "undefined") {
                that.Show({ Msg: "【MessageBox.OpenBlock】 请传入参数 TextID" });
                return;
            }
            option.Value = "?ID=" + option.Window.document.getElementById(option.ValueID).value;
            option.CallBack = function (index) {
                try {
                    var data = window.frames["layui-layer-iframe" + index].ModalData;
                    if (data.Text == "") {
                        data.Text = "请选择模板";
                    }
                    option.Window.document.getElementById(option.TextID).value = data.Text;
                    option.Window.document.getElementById(option.ValueID).value = data.Value;
                    return true;
                } catch (e) {
                    that.Show({ Msg: "【MessageBox.OpenBlock】 异常：" + e.toString() });
                    return false;
                }
            }
        }
        if (typeof option.Value == "undefined") {
            option.Value == null;
        }
        console.log(option);
        that.Load({
            Url: (that.Config.CheckBlock + (option.Value == null ? "" : option.Value)),
            Title: "选择模板",
            Area: ['650px', '450px'],
            CancelCallback: option.CallBack
        });
    };
    this.Alert = function (_option) {
        var option = {};
        option.Type = "alert";
        console.log(typeof _option);
        if (typeof _option == "string") {
            option.Msg = _option;
        }
        else {
            option.Msg = _option.Msg;
            option.YesCallBack = _option.YesCallBack;
            option.options = _option.options;
        }
        that.Show(option);
    };
    //跳转到登录页面
    this.RedirectLogin = function () {
        location.href = that.Config.RedirectLoginUrl;
    };
    //配置项
    this.Config = {
        //跳转登录页面
        RedirectLoginUrl: "/Login.aspx",
        //选择公司弹出框URL
        CheckCompanyUrl: "/UserCotrols/CompanyControl.aspx",
        //选择用户弹出框URL
        CheckUser: "/BusinessExpansion/PublicShow/SelectUser.aspx",
        //选择角色弹出框URL
        CheckRole: "/BusinessExpansion/Templete/ManageRole.aspx",
        //选择角色组弹出框URL
        CheckRoleTree: "/BusinessExpansion/PublicShow/SelectRoleGroup.aspx",
        //选择模板弹出框URL
        CheckBlock: "/BusinessExpansion/Templete/ManageBlock.aspx",
        //选择关键字弹出框URL
        CheckCode: "/BusinessExpansion/PublicShow/CreateCode.aspx",
        //编辑周队列
        EditMonthQueue: "/Modal/EditMonthQueue.aspx",
        //编辑队列条件
        EditCondition: "/Modal/EditCondition.aspx",
        //编辑队列
        EditQueue: "/Modal/EditQueue.aspx",
        //编辑模块
        EditModule: "/Modal/EditModule.aspx",
        //编辑模板
        EditBlock: "/Modal/EditBlock.aspx",
        //基础打印URL
        BasicPrint: "/BusinessExpansion/Print/MainPrint.aspx"
    };
};

var MessageBox = new _MessageBox();
//*初始化 开始
layer.config({
    //anim: 1, //默认动画风格
    skin: 'layui-layer-lan' //默认皮肤 demo-class layui-layer-lan
});

//兼容IE9 低版本
if (typeof console == "undefined") {
    console = {
        log: function (obj) {
            if (typeof obj == "object") {
                //MessageBox.Show({ Msg: JSON.stringify(obj), Type: "msg" });
            }
            else {
                //MessageBox.Show({ Msg: obj, Type: "msg" });
            }
        }
    };
}
(function () {
    //加载Thk
    var thk = {};
    if (window.thk == null || window.thk == undefined) {
        window.thk = thk;
    }
    else {
        thk = window.thk;
    }
    /**
     * 上传文件
     * @param options
     */
    thk.UploadFile = function (options) {
        //console.log(options.File);
        var formData = new FormData();
        formData.enctype = "multipart/form-data";
        formData.append('file', options.File);

        $.ajax({
            url: "/Module/StatisticalReport/api.ashx?Type=UPLOAD_FILE",
            type: "POST",
            contentType: false,
            cache: false,
            async: false,
            processData: false,
            scriptCharset: 'UTF-8',
            data: formData,
            success: function (result) {
                if (result.Type == 1) {
                    options.Success(result.Data);
                    return;
                }
                options.Erro(result.Msg);
                return;
            },
            erro: function () {
                options.Erro("上传接口异常请联系管理员!");
                return;
            }
        });
    };
    /**
     * 下载文件
     * @parm url 文件路径
     */
    thk.download = function (url) {
        jQuery('<form action="' + url + '" method="get" role="form" hidden="hidden">' +
            '</form>')
            .appendTo('body').submit().remove();
    }
    thk.BankMsg = {};
    var that = thk.BankMsg;
    /**
     * 状态 false 关闭,true 打开
     */
    that.Status = false;
    that.Layer = null;
    /**
     * 设置 状态
     */
    that.Dom = $("<div><div>");
    that.SetStatus = function (value) {
        if (value) {
            var p_dom = $("<div id=\"xzz\">这是一个弹出窗<a>122112</a></div>");
            var dom = $("#Layer-Bank");
            //dom.removeChild(0);
            console.log(dom[0]);
            dom[0].appendChild(p_dom[0])
            console.log(dom);
            //打开弹出窗
            var option = {
                type: 1,
                title: "银行流水未匹配项",
                content: p_dom,
                maxmin: true,
                end: function () {
                    that.SetStatus(false);
                },
                cancel: function (index) {
                    //alert("你还有未处理完的");
                    //return false;
                }
            };
            that.Layer = window.layer.open(option);
        }
        that.Status = value;
    };
    //获取 状态
    that.GetStatus = function () {
        return that.Status;
    };
    //运行函数
    that.Run = function () {
        setTimeout(Main, 1000);
        //循环函数每隔1000毫秒执行一次检测函数
        function Main() {
            if (!that.GetStatus) {
                that.Config.Action(null, 0, 1000, function (result) {
                    if (result.Type == 1) {
                        var data = result.Data;
                        if (data.length > 0) {
                            that.SetStatus = true;
                        }
                    }
                    setTimeout(Main, 1000);
                });
                return;
            }
            setTimeout(Main, 1000);
        }
    };
    that.Config = {
        Action: function (userId, skip, take, callback) {
            var data = {
                UserId: userId,
                Skip: skip,
                Take: take
            };
            $.ajax({
                url: "/Module/StatisticalReport/api.ashx?Type=GET_BANK_MSG",
                type: "POST",
                data: JSON.stringify(data),
                success: function (result) {
                    callback(result);
                },
                erro: function () {
                    callback({ Msg: "上传接口异常请联系管理员", Type: 0 });
                }
            });
        }
    }
    //    setTimeout(function(){
    //    console.log("xx");
    //    that.SetStatus(true);
    //    },3000);
}());
//*初始化 结束